/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.desktop.kungkhie.template.explorer;

import com.kungkhies.desktop.library.entities.Kungkhie;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.nodes.Children;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//com.kungkhies.desktop.kungkhie.template.explorer//KungkhieTemplateExplorer//EN",
autostore = false)
@TopComponent.Description(preferredID = "KungkhieTemplateExplorerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = false)
@ActionID(category = "Window", id = "com.kungkhies.desktop.kungkhie.template.explorer.KungkhieTemplateExplorerTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_KungkhieTemplateExplorerAction",
preferredID = "KungkhieTemplateExplorerTopComponent")
public final class KungkhieTemplateExplorerTopComponent extends TopComponent  implements ExplorerManager.Provider
{
    
    private static ExplorerManager em = new ExplorerManager();
    
    public KungkhieTemplateExplorerTopComponent()
    {
        initComponents();
        setName(NbBundle.getMessage(KungkhieTemplateExplorerTopComponent.class, "CTL_KungkhieTemplateExplorerTopComponent"));
        setToolTipText(NbBundle.getMessage(KungkhieTemplateExplorerTopComponent.class, "HINT_KungkhieTemplateExplorerTopComponent"));
        
        refreshNode();
        
        associateLookup(ExplorerUtils.createLookup(em, getActionMap()));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        beanTreeView1 = new org.openide.explorer.view.BeanTreeView();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(beanTreeView1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(beanTreeView1, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openide.explorer.view.BeanTreeView beanTreeView1;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened()
    {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p)
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p)
    {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
    
    public static void refreshNode()
    {
        EntityManager entityManager = Persistence.createEntityManagerFactory("KungkhieEntityLibraryPU").createEntityManager();
        Query query = entityManager.createQuery("SELECT k FROM Kungkhie k");
        List<Kungkhie> resultList = query.getResultList();
        em.setRootContext(new KungkhieTemplateRootNode(Children.create(new KungkhieTemplateChildFactory(resultList), true)));
    }

    @Override
    public ExplorerManager getExplorerManager()
    {
        return em;
    }
}
